<?php
/**
 * This example shows making an SMTP connection with authentication.
 */

require_once "PHPMailer-master/class.smtp.php";
require_once "PHPMailer-master/class.phpmailer.php";
/**
 * Class myMail
 * 对phpMailer进行一些基础设置
 * 用传进来的参数$arr设置邮件的标题、内容 、附件、收件人
 */
class myMail
{
    public $mail;
    //用传进来的参数$arr设置邮件的标题、内容 、附件、收件人
    public function setConfigs($arr){

        //多个地址用 ; 分隔
        if(isset($arr['addAddress'])){
            $adds=explode(';',$arr['addAddress']);
            foreach ($adds as $v)
                $this->mail->addAddress($v);
        }
        //邮件内容
        if(isset($arr['Body']))
            $this->mail->Body=$arr['Body'];

        //主题
        if(isset($arr['Subject']))
            $this->mail->Subject=$arr['Subject'];

        //多个附件用;分隔
        if(isset($arr['addAttachment'])){
            $atts=explode(";",$arr['addAttachment']);
            foreach ($atts as $v)
            $this->mail->addAttachment($v);
        }



    }

    /**
     * myMail constructor.
     * @param  $arr addAddress=>收件人 Body=>内容 Subject=>主题 atte
     */
    public  function __construct($arr)
    {

        //实例化PHPMailer核心类
        $this->mail = new PHPMailer;
        //使用smtp鉴权方式发送邮件
        $this->mail->isSMTP();
        //Enable SMTP debugging
        // 0 = off (for production use)
        // 1 = client messages
        // 2 = client and server messages
        $this->mail->SMTPDebug = 0;
        //方便HTML查看的DEBUG输出
        $this->mail->Debugoutput = 'html';
        //服务器地址
        $this->mail->Host = "smtp.163.com";
        //端口 25, 465 or 587
        $this->mail->Port = 25;
        $this->mail->CharSet='UTF-8';
        //是否验证用户
        $this->mail->SMTPAuth = true;
        //用户名
        $this->mail->Username = "chzhr01";
        //密码
        $this->mail->Password = "`12345";
        //设置发件人
        $this->mail->setFrom('chzhr01@163.com', 'C Z');
        //设置回复地址
        $this->mail->addReplyTo('chzhr01@163.com', 'C Z');
        //收件人
//        $this->mail->addAddress('czrll2@163.com', 'L L');

        //默认标题
        $this->mail->Subject = 'PHPMailer SMTP test';

        //默认内容

        $this->mail->Body="<h1>TEST</h1>";



        //设置其他参数
        $this->setConfigs($arr);

    }

    //发送邮件
    public function Start()
    {

//send the message, check for errors
        if (!$this->mail->send()) {
            echo "Mailer Error: " . $this->mail->ErrorInfo;
        } else {
            echo "Message sent!";
        }

    }
}